package com.csii.security;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.csii.user.entity.SysPermission;
import com.csii.user.entity.SysPosition;
import com.csii.user.entity.SysUser;
import com.csii.user.service.ISysPermissionService;
import com.csii.user.service.ISysPositionService;
import com.csii.user.service.ISysRoleService;
import com.csii.user.service.ISysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Auther: ian
 */
public abstract class AbstractUserDetailsService implements UserDetailsService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysPositionService sysPositionService;

    @Autowired
    private ISysPermissionService sysPermissionService;

   /* @Autowired
    private ISysRoleService sysRoleService;*/

    /**
     * 这个方法交给子类去实现它，查询用户信息
     * @param usernameOrMobile 用户名或者手机号
     * @return
     */
    public abstract SysUser findSysUser(String usernameOrMobile);

    @Override
    public UserDetails loadUserByUsername(String usernameOrMobile) throws UsernameNotFoundException {
        // 1. 通过请求的用户名去数据库中查询用户信息
        SysUser sysUser = findSysUser(usernameOrMobile);
        // 通过用户id去获取权限信息
        findSysPermission(sysUser);
        if(!StrUtil.isEmpty(sysUser.getPost())) {
            SysPosition position=sysPositionService.getOne(new LambdaQueryWrapper<SysPosition>().eq(SysPosition::getCode, sysUser.getPost()));
            if(position!=null)
            sysUser.setPostName(position.getName());
        }
        return sysUser;
    }

        private void findSysPermission(SysUser sysUser) {
        if(sysUser == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        Set<String> roles= sysUserService.getUserRolesSet(sysUser.getUsername());
        // 2. 查询该用户有哪一些权限
        List<SysPermission> permissions = sysPermissionService.queryByUser(sysUser.getUsername());
        if(CollectionUtils.isEmpty(permissions)&&CollectionUtils.isEmpty(roles)) {
            return ;
        }
        // 3. 封装权限信息
        List<GrantedAuthority> authorities = new ArrayList<>();
        for(SysPermission sp: permissions) {
            // 权限标识
            String code = sp.getPerms();
            if(!StringUtils.isEmpty(code)) {
                String[] pms=code.split(",|;");
                for (String pm:pms) {
                    if(!StringUtils.isEmpty(pm))
                    authorities.add(new SimpleGrantedAuthority(pm));
                }
            }
        }
        for(String r:roles)
        {
            if(!StringUtils.isEmpty(r))
            authorities.add(new SimpleGrantedAuthority("ROLE_"+r));
        }
        sysUser.setAuthorities(authorities);
    }

    public static void main(String[] args) {
        String code="online:goGenerateCode";
        String[] pms=code.split(",|;");
        for (String pm:pms) {
            if(!StringUtils.isEmpty(pm))
                System.out.println(pm);
        }
    }
}
